layui.use(['form', 'jquery', 'layer', 'table', 'upload'], function() {
	var form = layui.form,
		$ = layui.$,
		table = layui.table,
		layer = layui.layer,
		upload = layui.upload;
	// 下呼出销售属性菜单
	$('#sale_attr_pop_down' + goods_sign).on('click', function() {
		layer.open({
			type: 2,
			anim: -1,
			title: '添加销售属性',
			closeBtn: !1,
			offset: 'rb',
			shade: 0.1,
			shadeClose: !0,
			area: ['auto', '400px'],
			skin: "layui-anim layui-anim-du layui-layer-adminDown",
			content: sale_uri
		})
		return false;
	});
	if (goods_form_value.goods_id == undefined) {
		$('a[lay-event="site_data' + goods_sign + '"]').hide();
	} else {
		$('a[lay-event="site_data' + goods_sign + '"]').show();
	}
	// 初始化价格
	if (goods_form_value.sale_attr != undefined && goods_form_value.sale_attr.length > 0) {
		for (var i in goods_form_value.sale_attr) {
			var e = goods_form_value.sale_attr,
				field = {};
			field.sale_name = e[i].sale_attr_name;
			field.sale_value = e[i].sale_attr_value.replace(/,/g, '\n');
			field.sale_price = e[i].is_sale_price == 1 ? 'on' : '';
			create_sale_html(field, goods_sign, '', goods_form_value.sale_price_info.data);
		}
	} else {
		if (goods_form_value.sale_price_info != undefined) {
			create_sale_html([], goods_sign, '', goods_form_value.sale_price_info.data);
		} else {
			create_sale_html([], goods_sign, '');
		}
	}
	// 上传
	upload.render({
		elem: '#goods_pictures_upload' + goods_sign,
		url: upload_uri,
		accept: 'images',
		acceptMime: 'image/*',
		auto: true,
		field: 'goods_pictures_upload' + goods_sign,
		multiple: true,
		drag: true,
		before: function(obj) {
			layer.load();
		},
		done: function(res, index, upload) {
			if (res.code == 0) {
				var item = this.item
				if (item == undefined) {
					item = $('.layui-upload-drag');
				}
				var html = createUploadHtml('goods_pictures[]', res.data);
				item.prev('.layui-upload-list').append(html);
				layer.msg('上传成功', {
					icon: 1
				});
			} else {
				layer.msg(e.msg, {
					icon: 2
				});
			}

			layer.closeAll('loading');
		},
		error: function(index, upload) {
			layer.msg('网络错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});

	var lay_filter = 'tpl_goods' + goods_sign;
	//赋值
	form.val(lay_filter, goods_form_value);
	// 初始化图片
	if (goods_form_value.goods_pictures != undefined && goods_form_value.goods_pictures.length > 0) {
		for (var i in goods_form_value.goods_pictures) {
			var html = createUploadHtml('goods_pictures[]', goods_form_value.goods_pictures[i]);
			$('#layui-upload-list' + goods_sign).append(html);
		}
	}
	// 表单渲染
	form.render('checkbox');
	form.render('select');
	// 获取分类属性
	var option_value = new Array();
	var get_goods_attr = function(category_sign) {
		var ul = $('#goods_attr' + goods_sign);
		ul.html('');
		for (var k in goods_form_value.attr_value) {
			var e = goods_form_value.attr_value[k];
			html = '<div class="layui-form-item">' +
				'	<label class="layui-form-label">' + k + '</label>' +
				'	<label class="layui-form-label">' + e + '</label>' +
				'	</div>';
			ul.append(html);
		}
	};
	// 服务报价
	var get_service_price = function() {
		var ul = $('#service_spu' + goods_sign);
		ul.html('');
		for (var i in goods_form_value.goods_service) {
			var e = goods_form_value.goods_service[i];
			html = '<div class="layui-form-item"><input type="hidden" name="goods_service_id[]" value="' + e.goods_service_id +
				'" />' +
				'	<label class="layui-form-label" style="width:200px">' + e.service_name + '</label>' +
				'<div class="layui-input-block layui-col-md1 margin-left0"><input type="hidden" name="last_service_price[]" value="' +
				e.service_price + '">	<input type="number" placeholder="报价" name="service_price[]" value="' + e.service_price +
				'" lay-verify="number" autocomplete="off" class="layui-input sale_price"></div>' +
				'	<div class="layui-form-mid layui-word-aux">' + e.service_intro + '</div>' +
				'	</div>';
			ul.append(html);
		}
	};
	var get_accessory_price = function() {
		var ul = $('#accessory_spu' + goods_sign);
		ul.html('');
		for (var i in goods_form_value.goods_accessory) {
			var e = goods_form_value.goods_accessory[i];
			html = '<div class="layui-form-item"><input type="hidden" name="goods_accessory_id[]" value="' + e.goods_accessory_id +
				'" />' +
				'	<label class="layui-form-label" style="width:200px">' + e.accessory_name + '</label>' +
				'<div class="layui-input-block layui-col-md1 margin-left0"><input type="hidden" name="last_accessory_price[]" value="' +
				e.accessory_price + '">	<input type="number" placeholder="报价" name="accessory_price[]" value="' + e.accessory_price +
				'" lay-verify="number" autocomplete="off" class="layui-input sale_price"></div>' +
				'	<div class="layui-form-mid layui-word-aux">' + e.accessory_intro + '</div>' +
				'	</div>';
			ul.append(html);
		}
	};
	get_service_price();
	get_accessory_price();
	// 详情引用属性
	form.on('switch(quote_option)', function(obj) {
		var option_html = '';
		if (option_value.length > 0) {
			var th_title = '';
			var mod = 0;
			for (var i in option_value) {
				var attr_name = option_value[i]['attr_name'],
					value_type = option_value[i]['value_type'],
					attr_sign = option_value[i]['attr_sign'],
					attr_value = '';
				if (value_type == 1) {
					attr_value = $('input[name="' + attr_sign + '"]').val();
				} else {
					attr_value = $('select[name="' + attr_sign + '"]').val();
				}
				if (option_value[i]['group_name'] != th_title) {
					mod = 0;
					th_title = option_value[i]['group_name'];
					if (option_html != '') {
						option_html = option_html + '</tbody></table>';
					}
					option_html = option_html + '<table class="option_table"><tbody>';
					option_html = option_html + '<tr><th colspan="4">' + th_title + '</th></tr>';
				}
				if (mod == 0) {
					option_html = option_html + '<tr><td>' + attr_name + '：</td><td>' + attr_value + '</td>';
					mod = 1;
				} else {
					option_html = option_html + '<td>' + attr_name + '：</td><td>' + attr_value + '</td></tr>';
					mod = 0;
				}
			}
			option_html = option_html + '</tbody></table>';
			var get_um_html = um.getContent();
			var regex = /<table class="option_table"><tbody>[\s\S]*?<\/tbody><\/table>/g;
			var new_um_html = get_um_html.replace(regex, '');
			if (obj.elem.checked == true) {
				um.setContent(option_html + new_um_html);
			} else {
				um.setContent(new_um_html);
			}
		}
	});
	form.on('switch(push_to_site)', function(obj) {
		if (obj.elem.checked == true) {
			$(this).parent('div').next('div.layui-word-aux').show();
		} else {
			$(this).parent('div').next('div.layui-word-aux').hide();
		}
	});

	// 获取品牌
	var get_goods_brand = function(category_sign) {
		var e = $('form[lay-filter="' + lay_filter + '"]').find('#goods_brand_' + goods_sign);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		form.render('select');
		var post_data = {
			is_select: 'yes',
			category_sign: category_sign,
			_o: 'brand_id',
			_b: 'desc',
			page: 1,
			limit: 100
		};
		send_info(brand_select_uri, post_data, function(data) {
			e.html('<option value="">请选择</option>');
			if (data.count > 0) {
				for (var i in data.data) {
					var temp = data.data[i];
					if (goods_form_value.brand_sign == temp.brand_sign) {
						var op_html = '<option selected value="' + temp.brand_sign + '">' + temp.brand_name + '</option>';
					} else {
						var op_html = '<option value="' + temp.brand_sign + '">' + temp.brand_name + '</option>';
					}
					e.append(op_html);
				}
			};
			e.prop('disabled', false);
			form.render('select');
		})
	}

	// 初始化分类
	var get_goods_category = function(parent_sign, filter, value) {
		var e = $('form[lay-filter="' + lay_filter + '"]').find('#' + filter);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		var id = e.attr('id');
		form.render('select');
		var post_data = {
			parent_sign: parent_sign,
			audit_state: 1
		}
		send_info(category_select_uri, post_data, function(data) {
			e.html('<option value="">请选择分类</option>');
			for (var i in data.data) {
				var temp = data.data[i];
				if (value == temp.category_sign) {
					var op_html = '<option selected value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				} else {
					var op_html = '<option value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				}
				e.append(op_html);
			}
			e.prop('disabled', false);
			form.render('select');
			if (value != undefined) {
				if (id == 'category_level1_' + goods_sign) {
					get_goods_category(value, 'category_level2_' + goods_sign, goods_form_value.category_sign2);
					$('form[lay-filter="' + lay_filter + '"]').find('#category_level3_' + goods_sign).html('');
					$('form[lay-filter="' + lay_filter + '"]').find('#goods_brand_' + goods_sign).html('');
					get_goods_brand(value);
					get_goods_attr(value);
				} else if (id == 'category_level2_' + goods_sign) {
					get_goods_category(value, 'category_level3_' + goods_sign, goods_form_value.category_sign3);
					get_goods_brand(value);
					get_goods_attr(value);
				} else {
					get_goods_brand(value);
					get_goods_attr(value);
				}
			}
		})
	}
	var fliter1 = 'category_level1_' + goods_sign;
	// 分类联动
	form.on('select(category_level1_' + goods_sign + ')', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category(value, 'category_level2_' + goods_sign, goods_form_value.category_sign2);
		$('form[lay-filter="' + lay_filter + '"]').find('#category_level3_' + goods_sign).html('');
		$('form[lay-filter="' + lay_filter + '"]').find('#goods_brand_' + goods_sign).html('');
		get_goods_brand(value);
		get_goods_attr(value);
	})
	form.on('select(category_level2_' + goods_sign + ')', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_category(value, 'category_level3_' + goods_sign, goods_form_value.category_sign3);
		get_goods_brand(value);
		get_goods_attr(value);
	})
	form.on('select(category_level3_' + goods_sign + ')', function(data) {
		var value = data.value;
		var e = data.elem;
		get_goods_brand(value);
		get_goods_attr(value);
	})
	get_goods_category(0, 'category_level1_' + goods_sign, goods_form_value.category_sign1);

	// 初始化图片
	if (goods_form_value.goods_pictures_info) {
		var html = createUploadHtml('goods_pictures[]', goods_form_value.goods_pictures_info);
		$('#layui-upload-list' + goods_sign).html(html);
	}
form.on('switch',function(data){
		$(data.elem).attr('type', 'hidden').val(this.checked ? 1 : 2);
	})
	// 监听提交
	form.on('submit(submit_goods)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		send_info(url, field, function(data) {
			if (data.code == 1) {
				layer.msg('保存成功，准备推送数据，时间有点长', {
					icon: 1
				});
				layer.open({
					type: 2,
					anim: -1,
					title: !1,
					closeBtn: !1,
					offset: 'r',
					shade: 0.1,
					shadeClose: !0,
					area: '400px',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: push_henan_url + '?goods_sku=' + data.data
				})
				// 关闭当前页 刷新父页
				//$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				// 父页面点击
				//$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="product_list"]').trigger('click');
				// 表格重载
				table.reload('goods_list');
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})
	// 验证商品型号
	$(document).on('click', '.chk_goods_model', function() {
		var pdiv = $(this).parent('div'),
			value = pdiv.prev('div').find('input[name="goods_model"]').val(),
			msg_div = pdiv.next('div.layui-word-aux'),
			app_data = {
				goods_sign: goods_sign,
				value: value
			};
		if (app_data.value == '') {
			layer.msg('请先输入商品型号', {
				icon: 2,
				anim: 6
			});
			return false;
		}
		send_info(chk_goods_model_uri, app_data, function(data) {
			if (data.code == 1) {
				msg_div.html('<a class="tap-nav" lay-event="edit" data-id="edit_goods_' + data.data.goods_id + '" data-url="' +
					goods_form_url + '?goods_sku=' + data.data.goods_sku +
					'" data-title="修改商品" style="cursor: pointer;color:#FF5722">' + data.msg + '</a>');
			} else {
				msg_div.html('<span style="color:#5FB878">' + data.msg + '</span>');
			}
		})
		console.info(app_data);
		return false;
	})
	// 分站价格
	$(document).on('click', 'a[lay-event="site_data' + goods_sign + '"]', function() {
		layer.open({
			type: 2,
			anim: -1,
			title: '分站价格',
			closeBtn: true,
			offset: 'r',
			shade: 0.3,
			move: false,
			shadeClose: false,
			area: '90%',
			skin: "layui-anim layui-anim-rl layui-layer-adminRight",
			content: site_data_url + '?goods_sku=' + goods_sign
		})
		return true;
	})

})
